package com.compass.mobile.ui.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.ActivityCaseReportBinding
import com.compass.mobile.databinding.ActivitySpecialColumnBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.MedicalRecordReport
import com.compass.mobile.presenters.NurseItemPresenter
import com.compass.mobile.presenters.PicPresenter
import com.compass.mobile.widget.MyBridgeAdapter
import toJson

/**
 * Description： 病历详情
 * Date:2024/6/20 21:24
 */
class UploadReportDetailsActivity : BaseVBActivity<ActivityCaseReportBinding>() {


    companion object {
        fun start(context: Context, report: MedicalRecordReport?) {
            val intent = Intent(context, UploadReportDetailsActivity::class.java)
            intent.putExtra("report", report)
            context.startActivity(intent)
        }
    }

    private var report: MedicalRecordReport? = null

    private lateinit var arrayObjectAdapter: ArrayObjectAdapter
    override fun initView(savedInstanceState: Bundle?) {
        report = intent.serializable("report")
        mBinding.caseReportItem.post {
            // 使用获取到的宽度
            val width = mBinding.caseReportItem.width

            report?.let {
                specialColumn(width,it.img)
            }

        }


        LogUtil.d("http_data===${report?.toJson()}")
    }


    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {


        }
    }

    @SuppressLint("RestrictedApi")
    fun specialColumn(width:Int,imgList:MutableList<String>?) {
        val finalWidth = width - dpToPx(200)
        mBinding.caseReportItem.setNumColumns(4)
        mBinding.caseReportItem.horizontalSpacing = dpToPx(15)
        mBinding.caseReportItem.verticalSpacing = dpToPx(15)
        val picPresenter =
            PicPresenter()
        picPresenter.setFinalWidth(finalWidth / 4)
        arrayObjectAdapter = ArrayObjectAdapter(picPresenter)
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, onItemClickListener
        )
        mBinding.caseReportItem.setAdapter(itemBridgeAdapter)
        mBinding.caseReportItem.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
        arrayObjectAdapter.addAll(0, imgList)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
        mBinding.caseReportItem.requestFocus()


    }


}